package ie.lawlor.amvc.swing;

import ie.lawlor.amvc.IDialogEnabledView;
import ie.lawlor.amvc.IView;
import ie.lawlor.amvc.controller.Controller;
import ie.lawlor.amvc.event.Event;
import ie.lawlor.amvc.event.EventName;
import ie.lawlor.amvc.event.PopupPayload;

import java.awt.Container;

import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;

import org.apache.log4j.Logger;

public abstract class JPopupView extends javax.swing.JPopupMenu implements SwingView {
    
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	protected SwingViewProxy viewProxy;
	protected Logger logger;
	
	// Is not technically abstract, but should only be instanciated by a derived class. 
    protected JPopupView(String name) {
		viewProxy = new SwingViewProxy(name, this);
		logger = viewProxy.getLogger();
        addAncestorListener(new AncestorListener(){
            public void ancestorAdded(AncestorEvent e) {
            	viewProxy.setViewVisible(true);
            }
            public void ancestorMoved(AncestorEvent e) {
            }
            public void ancestorRemoved(AncestorEvent e) {
				viewProxy.setViewVisible(false);
            }
        });
    }
    
	public void eventIn(Event e) {
		viewProxy.eventIn(e);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.EventSource#fire(com.informatica.amvc.Event)
	 */
	public void fire(Event e) {
		viewProxy.fire(e);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.event.EventSource#fire(com.informatica.amvc.event.EventName, java.lang.Object)
	 */
	public void fire(EventName name, Object payload) {
		viewProxy.fire(name, payload);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.event.EventSource#fire(com.informatica.amvc.event.EventName)
	 */
	public void fire(EventName name) {
		viewProxy.fire(name);
	}

    /* (non-Javadoc)
     * @see com.informatica.amvc.View#setViewVisible(boolean)
     */
    public void setViewVisible(boolean visible) {
    	viewProxy.setViewVisible(visible);
    }

	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#addChild(com.informatica.amvc.View, java.lang.String)
	 */
	public void addChild(IView childView, String viewName) {
		viewProxy.addChild(childView, viewName);
	}

	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#addChild(com.informatica.amvc.View)
	 */
	public void addChild(IView childView) {
		viewProxy.addChild(childView);
	}
	
	public void addDialogChild(IDialogEnabledView childView) {
		viewProxy.addDialogChild(childView);
	}

    /* (non-Javadoc)
     * @see com.informatica.amvc.View#setParent(com.informatica.amvc.View)
     */
    public void setParent(IView view) {
    	viewProxy.setParent(view);
    }

    /* (non-Javadoc)
     * @see com.informatica.amvc.View#setParent(com.informatica.amvc.View, java.lang.String)
     */
    public void setParent(IView view, String viewName) {
    	viewProxy.setParent(view, viewName);
    }

	/* (non-Javadoc)
	 * @see com.informatica.amvc.swing.SwingView#getParentView()
	 */
	public IView getParentView() {
		return viewProxy.getParentView();
	}
	
	/* (non-Javadoc)
	 * @see com.informatica.amvc.swing.SwingView#getContainer(java.lang.String)
	 */
	public Container getContainer(String viewName) {
		return this;
	}
	
	/* (non-Javadoc)
	 * @see com.informatica.amvc.View#setController(com.informatica.amvc.Controller)
	 */
	public void setController(Controller controller) {
		viewProxy.setController(controller);
	}
	
	public Controller getController(){
		return viewProxy.getController();
	}
	
	public void doShowView(Event e){
    	//TODO Is this really needed?
		if (e.getPayload() instanceof PopupPayload){
			PopupPayload popupData = (PopupPayload)e.getPayload();
			setLocation(popupData.point);
			show(popupData.source, popupData.point.x, popupData.point.y);
		}else {
			show(this, 0, 0);
			setVisible(true);
		}
	}
    
	public void doHideView(Event e){
    	//TODO Is this really needed?
		setVisible(false);
	}
	
	public void doInitView(Event e) {
		//TODO Is there any need to implement this?
	}    
}
